﻿///@author Rafał Hazan
///@email  rafal.hazan@gmail.com
#ifndef __RANDOM_GENERATOR__
#define __RANDOM_GENERATOR__

#include <cstdlib>
#include <ctime>

/// Klasa generujaca losowe wartosci.
class RandomGenerator
{
	public:
		RandomGenerator()
		{
			srand ( (unsigned int)time(NULL) );
		}

		///@return wartosc losowa z przedzialu 0 do 1
		double getRandomToOne()
		{
			return rand() / (double)RAND_MAX;
		}

		/// Losuje liczbe z zakresu <start, end)
		int getRandom(int start, int end)
		{
			if (end != start)
			{
				int range = end - start;
				return start + rand() % range;
			}
			else
			{
				return start;
			}
		}

		/// Wybiera n losowych elementow z pojemnika, bez zwracania.
		std::set<int> nRandom(std::vector<int> vec, unsigned int n)
		{
			if (n > vec.size())
			{
				n = vec.size();
			}
			std::set<int> result;
			int stepNo = vec.size() - n;
			for (int step = vec.size() - 1; step >= stepNo; --step) 
			{
				int vIndex = getRandom(0, step);
				result.insert(vec[vIndex]);
				vec[vIndex] = vec[step];
			}
			return result;
		}

		std::set<int> nRandom(std::set<int> s, unsigned int n)
		{
			std::vector<int> v(s.begin(), s.end());
			return nRandom(v, n);
		}
};

#endif